import pymysql


def query(table_name):
    # 数据库连接配置
    connection = pymysql.connect(
        host='192.168.79.128',
        user='root',
        password='123456',
        port=3307,
        database='exam',
        charset='utf8',
        cursorclass=pymysql.cursors.DictCursor
    )
    try:
        with connection.cursor() as cursor:
            # 执行SQL查询
            sql = f"SELECT * FROM `{table_name}`"
            cursor.execute(sql)

            # 获取所有记录
            results = cursor.fetchall()
            # for row in results:
            #     print(row.get('id'))
            #     # print(f"ID: {row[0]}, Name: {row[1]}, Remark: {row[2]}")
            # subject_list = [item.get('id') for item in results]
            #print(results)
    finally:
        # 关闭数据库连接
        connection.close()
    return results

if __name__ == '__main__':
    print(query('student'))
    #print(query('subject'))